package org.piggy.core.filter.gray;

import org.piggy.common.constants.FilterConst;
import org.piggy.core.context.GatewayContext;
import org.piggy.core.filter.Filter;
import org.piggy.core.filter.FilterAspect;

@FilterAspect(id = FilterConst.GRAY_FILTER_ID, name = FilterConst.GRAY_FILTER_NAME, order = FilterConst.GRAY_FILTER_ORDER)
public class GrayFilter implements Filter {
    public static final String GRAY = "true";

    @Override
    public void doFilter(GatewayContext gatewayContext) {
        String gray = gatewayContext.getGatewayRequest().getHeaders().get("gray");
        if (GRAY.equals(gray)) {
            gatewayContext.setGray(true);
            return;
        }
        //选取部分的灰度用户
        String clientIp = gatewayContext.getGatewayRequest().getClientIp();
        int res = clientIp.hashCode() % 1024;
        if (res == 0) {
            gatewayContext.setGray(true);
        }
    }
}

